package com.onjob.security;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import util.dao.HibernateDaoImpl;
import util.dao.Pagination;

public class UsersDaoImpl extends HibernateDaoImpl<Users,Long>implements UsersDao {


	public Users findByUsers(Users user) {
		
	Session session =null;
	session = this.getSession();
	Query query = session.createQuery("from " +Users.class.getName()+" u where " +
				"u.userName=? and u.userPassword=?");
		 query.setString(0,user.getUserName());
         query.setString(1, user.getUserPassword());
		 
		return (Users)query.uniqueResult();

		
	}

	@Override
	public Users findByEmail(String email) {
		Session session =null;
		session = this.getSession();
		Query query = session.createQuery("from " +Users.class.getName()+" u where " +
					"u.userEmail=?");
			 query.setString(0,email);
	       
			return (Users)query.uniqueResult();

	}

	@Override
	public Users findByUserName(String uname) {
		Session session =null;
		session = this.getSession();
		Query query = session.createQuery("from " +Users.class.getName()+" u where " +
					"u.userName=?");
			 query.setString(0,uname);
	       
			return (Users)query.uniqueResult();
	
	}

	  @Override
		public boolean isUniqueName(final String userEmail) {
			// TODO Auto-generated method stub
			return(Boolean)this.getHibernateTemplate().execute(new HibernateCallback(){

				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					// TODO Auto-generated method stub
					Query query=session.createQuery("from "+Users.class.getName()+" u where u.userEmail=?");
					query.setParameter(0,userEmail);
					return query.uniqueResult()==null;
				}
				
			});
		}


	


}
